﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using K9Nano.Application.Organization;
using K9Nano.Application.Organization.Dto;
using K9Nano.Share;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace K9Nano.Web.Organization
{
    [ApiController]
    [Route("org/[Action]")]
    [Authorize(Policy = K9SharedConstants.PolicyTenantAdmin)]
    public class OrganizationUnitController : ControllerBase
    {
        private readonly OrganizationUnitAppService _organizationUnitAppService;

        public OrganizationUnitController(OrganizationUnitAppService organizationUnitAppService)
        {
            _organizationUnitAppService = organizationUnitAppService;
        }

        [HttpPost]
        public async Task<OrganizationUnitDto> Create(OrganizationUnitCreateInput input)
        {
            return await _organizationUnitAppService.CreateAsync(input, HttpContext.RequestAborted);
        }

        [HttpPut]
        public async Task<OrganizationUnitDto> Update(OrganizationUnitUpdateInput input)
        {
            return await _organizationUnitAppService.UpdateAsync(input, HttpContext.RequestAborted);
        }

        [HttpDelete]
        public async Task Delete(Guid id)
        {
            await _organizationUnitAppService.DeleteAsync(id, HttpContext.RequestAborted);
        }

        [HttpGet]
        [Route("/org/children")]
        public async Task<IReadOnlyList<OrganizationUnitDto>> GetChildren(Guid? parentId, bool recursive = false)
        {
            return await _organizationUnitAppService.GetChildrenAsync(parentId, recursive, HttpContext.RequestAborted);
        }

        [HttpGet]
        [Route("/org/{id}")]
        public async Task<OrganizationUnitDto> Get(Guid id)
        {
            return await _organizationUnitAppService.GetAsync(id, HttpContext.RequestAborted);
        }
    }
}